<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-search-history')">全部删除</span>
        &nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon name="delete" v-else @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      :title="v"
      v-for="(v, i) in searchHistories"
      :key="i"
      @click="onSearchItemClick(v, i)"
    >
      <van-icon name="close" v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  data() {
    return {
      isDeleteShow: false,
    };
  },
  props: {
    searchHistories: {
      type: Array,
      required: true,
    },
  },
  methods: {
    onSearchItemClick(v, i) {
      if (this.isDeleteShow) {
        this.searchHistories.splice(i, 1);
      } else {
        this.$emit('search', v);
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
